#!/usr/bin/perl
##
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
# -- 05/26/2009 Created by Zhixin Li
# -- Used to count the increment of mysql binlog size in constant interval
# -- check if log_bin set, 05/28/2009, modified by Zhixin Li
# -- change tmpfile name,add $PORT as unique sign, 06/25/2009, modified by Zhixin Li 
# -- temporary file : ~oracle/.mysqlbin_PORT.log ,record latest binlog name and corresponding log position
use strict;
my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $SQL="show binary logs";
my $SQL2="show variables like 'log_bin'";
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Status${DELIM}Down\n";
    exit 1;
}

my $binstat =`$MYSQL -N -s $PARAM -e "$SQL2"`;
my $binset = substr($binstat,index($binstat,"\t")+1,length($binstat)-index($binstat,"\t")+1);
chomp($binset);
if ( "$binset" ne "ON" ) {
    #print "log_bin not set\n";
    exit 0;
}

my $LogDir = $ENV{HOME};
my $TMPFILE = "$LogDir/.mysqlbin_$PORT.log";

# Execute command and display the result as key-value pairs
my @KV=`$MYSQL -N -s $PARAM -e "$SQL"`;

my $BinLogInc = 0;
my $sum = 0;
my $rownum = scalar(@KV);
my @curstat = split /\t/,@KV[${rownum}-1];
my $curlog = $curstat[0];
my $cursize = $curstat[1];
chomp(@curstat);
if (-s $TMPFILE) {
    open(MYFILE,"$TMPFILE") or die "Couldn't open $TMPFILE for reading: $!\n"; 
    my @array = <MYFILE>;
    my @field = split / +/,$array[0];
    chomp(@field);
    my $lastlog = $field[0];
    my $lastsize = $field[1];
    my $position = index($lastlog,".");
    my $logsn = substr($lastlog,${position} + 1);
    close(MYFILE);

    chomp(@KV);
    for (@KV) {
       my ($key, $value) = split /\t/, $_;
       my $newlog = substr(${key},${position} + 1);
       if( ${newlog} ge ${logsn} ){
         $sum=$sum + ${value};
       }    
       $BinLogInc = $sum - $lastsize;
     }
    print "em_result=BinLogInc${DELIM}${BinLogInc}\n";
    open(OUTFILE,">$TMPFILE") or die "Couldn't open $TMPFILE for writing: $!\n";
    print OUTFILE ("@curstat\n");
} else {
    my $position = index($curlog,".");
    my $logsn = substr($curlog,${position} + 1);
    chomp(@KV);
    for (@KV) {
       my ($key, $value) = split /\t/, $_;
       my $newlog = substr(${key},${position} + 1);
       if( ${newlog} >= 0 ){
          $sum=$sum + ${value};
       }
       $BinLogInc = $sum ;
    }
    print "em_result=BinLogInc${DELIM}${BinLogInc}\n";
    open(OUTFILE,">$TMPFILE") or die "Couldn't open $TMPFILE for writing: $!\n";
    print OUTFILE ("@curstat\n");
}      
exit 0;
